# -*- coding: utf-8 -*-

from enum import Enum
from typing import List


class CellAlign(Enum):
    LEFT = 1
    MIDDLE = 2
    RIGHT = 3


class MarkdownCell(object):
    val: str
    align: CellAlign

    def __init__(self, val: str, align=CellAlign.LEFT):
        self.val = val
        self.align = align


class MarkdownRow:
    cells: List[MarkdownCell]
    column_size: int
    is_header: bool

    def __init__(self, column_size: int, is_header=False):
        self.cells = []
        self.column_size = column_size
        self.is_header = is_header

    def add_cell(self, cell: MarkdownCell):
        if len(self.cells) == self.column_size:
            raise ValueError('row cells is already maximum')
        self.cells.append(cell)

    def add_cell_val(self, cell_val: str):
        self.add_cell(MarkdownCell(cell_val))

    def add_cells(self, *cell_list: str):
        if len(cell_list) > self.column_size:
            raise ValueError(f'row cells exceed maximum: {self.column_size}')
        for item in cell_list:
            self.add_cell_val(item)

    def make_row(self):
        def inner_make_row(data: List[str]):
            inner_res = '| '
            inner_res += ' | '.join(data)
            return inner_res + ' |\n'

        res = ''
        if self.is_header:
            header_elements = []
            split_elements = []
            for item in self.cells:
                header_elements.append(item.val)
                if item.align == CellAlign.LEFT:
                    split_elements.append(':--')
                elif item.align == CellAlign.MIDDLE:
                    split_elements.append(':--:')
                else:
                    split_elements.append('--:')

            res += inner_make_row(header_elements)
            res += inner_make_row(split_elements)
        else:
            res += inner_make_row([item.val for item in self.cells])
        return res


class MarkdownTable:
    column_size: int
    header: MarkdownRow
    data: List[MarkdownRow]

    def __init__(self, column_size: int):
        self.column_size = column_size
        self.data = []

    def set_header(self, header: MarkdownRow):
        self.header = header

    def add_row(self, row: MarkdownRow):
        if row is not None:
            pass
        self.data.append(row)

    def make(self):
        if self.header is None:
            raise TypeError('header is missed for the markdown table ')

        res_str = self.header.make_row()
        for data_row in self.data:
            res_str += data_row.make_row()
        return res_str
